package com.test.SocketsTest;

import java.io.IOException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.view.*;
import android.os.Bundle;
import android.widget.EditText;

public class SocketsTestActivity extends Activity {
	private SocketsClient commDriver;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setContentView(R.layout.transition_layout);
        super.onCreate(savedInstanceState);
        initializeNetwork();        

    }
    private void initializeNetwork() {
    	try {
			commDriver = new SocketsClient("192.168.2.4"); //Replace with host IP.
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	setContentView(R.layout.main);
	}
	public void sendText(View v) throws IOException
    {
    	byte[] toSend; 
    	char[] toRecieve;
    	EditText myEntryField = (EditText)findViewById(R.id.editText1);
    	toSend = myEntryField.getText().toString().getBytes();
    	toRecieve = commDriver.sendData(toSend);
    	myEntryField.setText("");
    	myEntryField = (EditText)findViewById(R.id.editText2);
    	myEntryField.setText(String.copyValueOf(toRecieve));
    	
    }
    public void clearReplies(View v)
    {
    	EditText myRecievedField = (EditText)findViewById(R.id.editText2);
    	myRecievedField.setText("Text cleared!");
    }
    
    public void cleanUpAndQuit(View v)
    {
    	try {
			commDriver.closeConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	System.exit(0);
    }

}